package com.twu.biblioteca;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Jaani Francis Nickolas
 * Date: 19/9/13
 * Time: 11:34 PM
 * To change this template use File | Settings | File Templates.
 */
public class MainLibrary {
    public static int Reception() throws IOException {
        System.out.println("************************************************");
        System.out.println("                  Library Menu                  ");
        System.out.println("(1) Browse Books");
        System.out.println("(2) Reserve Books");
        System.out.println("(3) Browse Movies");
        System.out.println("(4) My Profile");
        System.out.println("************************************************");
        System.out.print("Choice: ");
        DataInputStream dis_choice = new DataInputStream(System.in);
        String menuChoice = dis_choice.readLine();
        System.out.println("************************************************");
        return Integer.parseInt(menuChoice);
    }

    public static void main(String[] args) throws IOException {

        MyLibrary BPL = new MyLibrary();
        DataInputStream disUserChoice = new DataInputStream(System.in);
        DataInputStream disNoOfBooks = new DataInputStream(System.in);
        DataInputStream disBookId = new DataInputStream(System.in);
        DataInputStream disUserName = new DataInputStream(System.in);
        DataInputStream disPassword = new DataInputStream(System.in);
        String userChoice;
        String temporaryNumberOfBooks;
        String temporaryBookId;
        String temporaryUserName;
        String temporaryPassword;
        int menuChoice;
        boolean grantAccess;

        System.out.println("************************************************");
        System.out.println("       Welcome to Bangalore Public Library      ");

        do
        {
            grantAccess = false;
            System.out.print("Your Username: ");
            temporaryUserName = disUserName.readLine();
            System.out.print("Your Password: ");
            temporaryPassword = disPassword.readLine();
            if (BPL.checkUserDetails(temporaryUserName,temporaryPassword) == true)
            {
                System.out.println(" >> Valid Credentials");
                grantAccess = true;
            }
            else
                System.out.println(" >> Invalid Credentials");
            menuChoice = Reception();
            switch(menuChoice)
            {
                case 1: // Browse all books -- Menu
                    BPL.displayLibraryBooks();
                    break;

                case 2: // Reserve books -- Menu
                    if (grantAccess == true)
                    {
                        System.out.print(" >> Number of books you want to reserve: ");
                        temporaryNumberOfBooks = disNoOfBooks.readLine();
                        int noOfBooks = Integer.parseInt(temporaryNumberOfBooks);
                        for(int i = 0;i < noOfBooks;i++)
                        {
                            System.out.print("    " + (i+1) + ". Enter the book id: ");
                            temporaryBookId = disBookId.readLine();
                            if(BPL.reserveBook(temporaryBookId) == true)
                                System.out.println("       Thank You! Enjoy the book.");
                            else
                                System.out.println("       Sorry we don't have that book yet.");
                        }
                    }
                    else
                    {
                        System.out.println("Access Denied!");
                    }
                    break;

                case 3: // Browse all Movies -- Menu
                    BPL.displayMovies();
                    break;

                case 4: // My Profile -- Menu
                    if (grantAccess == true)
                    {
                        BPL.displayUserDetails(temporaryUserName,temporaryPassword);
                    }
                    else
                    {
                        System.out.println("Access Denied!");
                    }
                    break;

                default:
                    System.out.println("Select a valid option!");
            }
            System.out.println("************************************************");
            System.out.print("Do you wish to continue to Library Menu? (yes/no): ");
            userChoice = disUserChoice.readLine();

        }while(userChoice.equalsIgnoreCase("yes"));
    }
}
